<div class="container-fluid">
    <div class="wrapper">
        <div class="panel" id="nav">
            <div class="panel-heading"><h3 class="panel-title">返仓入库表</h3></div>
            <div class="panel-body">
                <form action="__URL__/shipmentIdBackList" method="get">
                    <div class="container-fluid">
                        <div class="col-sm-12 col-md-3  add-space">
                            <div class="input-group input-group-md">
                                <span class="input-group-addon">shipmentid</span>
                                <input class="form-control" type="text" name="shipmentid" value="">
                            </div>
                        </div>
                        <div class="col-sm-12 col-md-2 add-space">
                            <div class="input-group input-group-md">
                                <span class="input-group-addon">账号</span>
                                <select class="chosen-select form-control" tabindex="-1" name="account_id"
                                        data-name-group="common">
                                    <option value="">请选择</option>
                                    <volist name="accounts" id="acc">
                                        <option value="{$acc.id}">{$acc.name}</option>
                                    </volist>
                                </select>
                            </div>
                        </div>
                        <div class="col-sm-12 col-md-2 add-space">
                            <div class="input-group input-group-md">
                                <span class="input-group-addon">销售员</span>
                                <select class="chosen-select form-control" tabindex="-1" name="seller_id"
                                        data-name-group="common">
                                    <option value="">请选择</option>
                                    <volist name="sellers" id="sl">
                                        <option value="{$Think.lang.$key}">{$sl}</option>
                                    </volist>
                                </select>
                            </div>
                        </div>
                        <div class="col-sm-12 col-md-2 add-space">
                            <div class="input-group input-group-md">
                                <span class="input-group-addon">状态</span>
                                <select class="chosen-select form-control" tabindex="-1" name="back_status"
                                        data-name-group="common">
                                    <option value="">请选择</option>
                                    <volist name="back_status" id="bs">
                                        <option value="{$Think.lang.$key}">{$bs}</option>
                                    </volist>
                                </select>
                            </div>
                        </div>
                        <div class="col-sm-12 col-md-3  add-space">
                            <div class="input-group input-group-md">
                                <span class="input-group-addon">SKU</span>
                                <input class="form-control" type="text" name="sku" value="">
                            </div>
                        </div>
                        <div class="col-sm-12 col-md-6  add-space">
                            <div class="input-group input-group-md">
                                <span class="input-group-addon">返仓时间</span>
                                <input class="form-control form-datetime" type="text" name="actual_back_time_from"
                                       style="cursor:pointer;" readonly>
                                <span class="input-group-addon"><span class="icon-calendar"></span></span>
                                <input class="form-control form-datetime" type="text" name="actual_back_time_to"
                                       style="cursor:pointer;" readonly>
                            </div>
                        </div>
                        <div class="col-md-2 add-space">
                            <input class="btn btn-primary" type="submit" value="搜索">
                            <input class="btn btn-success" type="submit" value="导出">
                        </div>
                    </div>
                </form>
                <div class="clearfix"></div>
                <hr>
                <table class="table table-striped  table-hover" cellspacing="0" width="100%">
                    <thead>
                    <tr>
                        <th width="20"><input id="checkAll" type="checkbox"></th>
                        <th>序号</th>
                        <th>状态</th>
                        <th>shipmentid</th>
                        <th>账号</th>
                        <th>SKU</th>
                        <th width="200">产品名称</th>
                        <th>数量</th>
                        <th>分配储位</th>
                        <th>是否退税</th>
                        <th>公司主体</th>
                        <th>销售员</th>
                        <th>申请人</th>
                        <th>申请时间</th>
                        <th>审核时间</th>
                        <th>发货仓库</th>
                        <th>返仓时间</th>
                        <th>备注</th>
                    </tr>
                    </thead>
                    <tbody>
                    <volist name="data" id="vo" key="num">
                        <tr>
                            <td class="text-center"><input name="subBox" type="checkbox" value="{$vo.id}"></td>
                            <td>{$num}</td>
                            <td class="back_status"><input type="hidden" name="status" value="{$vo.back_status}">{$vo.back_status_name}</td>
                            <td class="back_shipmentId"><input type="hidden" name="backShipment" value="{$vo.shipmentid}">{$vo.shipmentid}</td>
                            <td>{$vo.account_name}</td>
                            <td>{$vo.sku}</td>
                            <td>{$vo.sku_name}</td>
                            <td class="need_qty">{$vo.back_quantity}</td>
                            <td>
                                <if condition="($vo.back_status eq 20) AND ($success eq 1)">
                                    <a href="javascript:void(0);" class="addPosition" data-toggle="modal" data-target=".addRelation">分配储位</a>
                                    <else />
                                    分配储位
                                </if>
                            </td>
                            <td><if condition="$vo.export_tax_rebate eq 1">是<else />否</if></td>
                            <td>{$vo.enterprise_dominant_name}</td>
                            <td>{$vo.seller_name}</td>
                            <td>{$vo.back_apply_user_name}</td>
                            <td>{$vo.back_apply_time}</td>
                            <td>{$vo.confirm_time}</td>
                            <td><if condition="$vo.tranfer_hopper_id eq 1">东莞高埗仓<else />其他</if></td>
                            <td>{$vo.actual_back_time}</td>
                            <td>{$vo.reason}</td>
                        </tr>
                    </volist>
                    </tbody>
                </table>
                <div class="clearfix clear">
                </div>
                <!--<div class="page page-wrapper">
                    {$page}
                </div>--><div class="row">
                <div class="col-md-6 pull-left">
                    <label>每页 20 条记录
                        显示 1 到 20 项，共 {$count}项</label>
                </div>
                <div class="col-md-6">
                    <div class="page pull-right" style="padding: 0;margin: 0">
                        {$page}
                    </div>
                </div>
                <div class="clearfix clear"></div>
                <div class="col-md-offset-5 col-md-2 add-space">
                    <if condition="($role_id eq 1) OR ($role_id eq 3)">
                        <input class="btn btn-success" type="button" onclick="examine()" value="审核">
                        <!--<input class="btn btn-success" type="button" onclick="alert('没有权限')" value="审核">-->
                    </if>

                    <!--<input class="btn btn-primary" type="button" onclick="saveBackShipment()" value="确认返仓">-->
                </div>
            </div>
        </div>
    </div>
    </div>
</div>
<div class="modal fade addRelation" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <form action="__URL__/saveBackShipment" id="save" method="post" onsubmit="return checkPosition()">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">分配储位</h4>
                </div>
                <div class="modal-body">
                    <table class="table table-striped  table-hover" cellspacing="0" width="100%">
                        <thead>
                        <tr>
                            <th>储位</th>
                            <th>数量</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody id="addHtml">
                        <tr>
                            <td><input type="text" name="position[]" class="form-control" /></td>
                            <td><input type="text" name="quantity[]" class="form-control" /></td>
                            <td><a href="javascript:add();">增加</a></td>
                        </tr>
                        </tbody>
                    </table>
                </div>
                <div class="modal-footer">
                    <input type="hidden" value="" name="need_qty">
                    <input type="hidden" value="" name="backId">
                    <button type="submit" class="btn btn-primary save">确定</button>
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                </div>
            </form>
        </div>
    </div>
</div>
<script>
    $(function () {
        $("input[name='shipmentid']").val("<?php  echo $_GET['shipmentid'];?>");
        $("input[name='sku']").val("<?php  echo $_GET['sku'];?>");
        $("select[name='back_status']").val("<?php echo $_GET['back_status'];?>");
        $("select[name='account_id']").val("<?php echo $_GET['account_id'];?>");
        $("select[name='seller_id']").val("<?php echo $_GET['seller_id'];?>");
        $("input[name='actual_back_time_from']").val("<?php  echo $_GET['actual_back_time_from'];?>");
        $("input[name='actual_back_time_to']").val("<?php  echo $_GET['actual_back_time_to'];?>");
        $('select.chosen-select').chosen({
            no_results_text: '没有找到',    // 当检索时没有找到匹配项时显示的提示文本
            disable_search_threshold: 2, // 10 个以下的选择项则不显示检索框
            search_contains: true         // 从任意位置开始检索
        });


        $(document).on("click", ".storageQtyDel", function(){
            $(this).parent().parent().remove();
        });
    });
    $('.addPosition').click(function () {
        var id = parseInt($(this).parent('td').siblings('.text-center').find('input').val());
        var qty = parseInt($(this).parent('td').siblings('.need_qty').text());
        $("input[name='backId']").val(id);
        $("input[name='need_qty']").val(qty);
    });
    function add() {
        $("#addHtml").append('<tr><td><input type="text" name="position[]" class="form-control" /></td><td><input type="text" name="quantity[]" class="form-control" /></td><td><a href="javascript:add();">增加</a>  <a class="storageQtyDel" style="cursor: pointer">删除</a></td></tr>');
    }
    //审核
    function examine() {
        var id = [];
        var msg = '';
        var shipmentId = '';
        $('input[name="subBox"]:checked').each(function(){
            var status = $(this).parent('td').siblings('.back_status').find('input').val();
            shipmentId = $(this).parent('td').siblings('.back_shipmentId').find('input').val();
            if($.trim(status)!=10){
                msg += shipmentId+'的状态不能审核</br>';
            }
            id.push($(this).val());
        });
        if(msg!=''){
            layer.msg(msg, {icon: 5});
            return false;
        }
        if(id==''){
            layer.msg('请选择要审核的数据', {icon: 5});
            return false;
        }
        if(confirm('你确定要审核吗？')==true) {
            $.ajax({
                url: "__URL__/examineBackShipment",
                type: 'post',
                async: true, //default: true
                data: {'id':id},
                dataType: 'json', // default: Intelligent Guess (xml, json, script, or html)
                success: function (data, textStatus, jqXHR) {
                    //alert(data)
                    if ($.trim(data.status) == 200) {
                        layer.msg(data.msg);
                        window.location.reload();
                    }
                    else{
                        layer.msg(data.msg, {icon: 5});
                    }
                }
            });
        }
    }
    //分配储位检查
    function checkPosition() {
        var msg = '';
        var flag = '';
        var qty = 0;
        var position = [];
        var need_qty = $("input[name='need_qty']").val();
        $('input[name="position[]"]').each(function(){
            if($(this).val()==''){
                msg +='储位不能为空<br>';
            }else{
                position.push($(this).val());
            }
        });
        $('input[name="quantity[]"]').each(function(){
            if($(this).val()==''){
                msg +='数量不能为空<br>';
            }else if(isNaN($(this).val())){
                msg +='数量必须填写数字<br>';
            }
            else{
                qty += parseInt($(this).val());
            }
        });
        if(msg!=''){
            layer.msg(msg, {icon: 5});
            return false;
        }
        if(qty>need_qty){
            layer.msg('分配数量大于实际数量', {icon: 5});
            return false;
        }
        if(qty<need_qty){
            layer.msg('分配数量小于实际数量', {icon: 5});
            return false;
        }
        $.ajax({
            url: "__URL__/checkPosition",
            type: 'post',
            async: false, //default:
            data: {'position':position},
            dataType: 'json', // default: Intelligent Guess (xml, json, script, or html)
            success: function (data) {
                if ($.trim(data.status) == 200) {
                    flag = 'ok';
                }else{
                    layer.msg(data.msg, {icon: 5});
                    flag = 'no';
                }
            }
        });
        if(flag == 'no'){
            return false;
        }
        return true;
    }
</script>